package com.wyz.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.wyz.common.dto.OrderDTO;
import com.wyz.common.util.Message;
import com.wyz.web.service.OrderService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("/order")
//@CrossOrigin(origins = {"http://127.0.0.1", "http://127.0.0.1/"}, allowCredentials = "true")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private HttpSession sess;

    /**
     * 返回全部未接单的订单，在主页上显示
     */
    @GetMapping(value = "/all")
    public Message getAllUnpickOrders() {
        return Message.succeed(orderService.getAllUnpickOrders());
    }

    /**
     * 获取订单详情
     */
    @GetMapping(value = "/detail")
    public Message getOrderDetail(@RequestParam("orderId") long orderId) throws JsonProcessingException {
        log.info("请求订单号：{} 的详细信息", orderId);
        return Message.succeed(orderService.getOrderDetail(orderId));
    }



    /* start发单功能 */
    /**
     * 查询自己发起的订单
     */
    @GetMapping(value = "/sender/all", produces = "application/json")
    public Message getSendedOrders() {
        long userId = (long) sess.getAttribute("userId");
        return Message.succeed(orderService.getOrdersBySenderId(userId));
    }

    /**
     * 发布订单
     */
    @PostMapping(value = "/sender/publish")
    public Message publishOrder(@RequestBody OrderDTO orderDTO) {
        log.info("提交订单：{}", orderDTO);
        long userId = (long) sess.getAttribute("userId");
        return Message.succeed(orderService.publishOrder(userId, orderDTO));
    }

    /**
     * 取消发布订单
     */
    @RequestMapping(value = "/sender/cancel")
    public void cancelPublishOrder(@RequestParam("orderId") long orderId) {
        orderService.cancelPublishOrder(orderId);
    }

    /**
     * 确认订单完成
     */
    @RequestMapping(value = "/sender/confirm")
    public void confirmOrder(@RequestParam("orderId") long orderId) {
        orderService.confirmOrder(orderId);
    }
    /* end发单功能 */

    /* start接单功能 */
    /**
     * 查询自己接的订单
     */
    @GetMapping(value = "/receiver/all")
    public Message getPickedOrders() {
        long userId = (long) sess.getAttribute("userId");
        return Message.succeed(orderService.getPickedOrdersByReceiverId(userId));
    }

    /**
     * 接单：只需要改订单的状态
     */
    @RequestMapping(value = "/receiver/pick")
    public void pickOrder(@RequestParam("orderId") long orderId) {
        long userId = (long) sess.getAttribute("userId");
        orderService.pickOrder(userId, orderId);
    }

    /**
     * 接单者-取消接单：只需要改订单的状态
     */
    @RequestMapping(value = "/receiver/cancel")
    public void cancelPickOrder(@RequestParam("orderId") long orderId) {
        orderService.cancelPickOrder(orderId);
    }

    /**
     * 接单者-确认送达
     */
    @RequestMapping(value = "/receiver/confirm")
    public void confirmOrderArrived(@RequestParam("orderId") long orderId) {
        orderService.confirmOrderArrived(orderId);
    }
    /* end接单功能 */
}
